// pages/post/post-detail/post-detail.js

import {
  DBPost
} from '../../../db/DBPost.js';

//获取全局变量;
var app = getApp();
console.log(app);

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlayingMusic: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var postId = options.id;
    this.dbPost = new DBPost(postId);
    this.postData = this.dbPost.getPostItemById().data;
    this.setData({
      post: this.postData
    });

    this.addReadingTimes();
    this.setMusicMonitor();
    this.initMusicStatus();
    this.setAniation();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    wx.setNavigationBarTitle({
      title: this.postData.title,
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    //停止播放;
    //屏蔽以支持全局播放;
    /*wx.stopBackgroundAudio();
    this.setData({
      isPlayingMusic:false
    })*/
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
      return {
        title:this.postData.title,
        desc:this.postData.content,
        path:"/pages/post/post-detail/post-detail"
      }
  },

  onCollectionTap: function(event) {
    var newData = this.dbPost.collect();

    // 有选择地更新部分数据
    this.setData({
      'post.collectionStatus': newData.collectionStatus,
      'post.collectionNum': newData.collectionNum
    })

    wx.showToast({
      title: newData.collectionStatus ? "收藏成功" : "取消成功",
      duration: 1000,
      icon: "success",
      mask: true
    })
  },

  onUpTap: function(event0) {
    var newData = this.dbPost.up();

    this.setData({
      'post.upStatus': newData.upStatus,
      'post.upNum': newData.upNum
    })

    this.animationUp.scale(2).step();  //放大；
    this.setData({
      animationUp:this.animationUp.export() //导出并清空动画；
    })
    setTimeout(function(){
      this.animationUp.scale(1).step();  //复原；
      this.setData({
        animationUp:this.animationUp.export()
      })
    }.bind(this), 300);

    wx.showToast({
      title: newData.upStatus ? "点赞成功" : "取消成功",
      duration: 1000,
      icon: "success",
      mask: true
    })
  },

  onCommentTap: function(event) {
    var id = event.currentTarget.dataset.postId;
    wx.navigateTo({
      url: '../post-comment/post-comment?id=' + id
    })
  },

  //阅读量+1
  addReadingTimes: function() {
    this.dbPost.addReadingTimes();
  },

  onMusicTap: function(event) {

    if (this.data.isPlayingMusic) {
      wx.pauseBackgroundAudio(); //暂停;
      this.setData({
        isPlayingMusic: false
      });

      app.globalData.g_isPlayingMusic = false;
    } else {
      //播放;
      wx.playBackgroundAudio({
        dataUrl: this.postData.music.url,
        title: this.postData.music.title,
        coverImgUrl: this.postData.music.coverImgUrl
      })
      this.setData({
        isPlayingMusic: true
      });

      app.globalData.g_isPlayingMusic = true;
      //以文章ID作为音乐Id
      app.globalData.g_currentMusicPostId = this.postData.postId;
    }
  },

  //监听音乐停止事件;
  setMusicMonitor: function() {
    var that = this;
    wx.onBackgroundAudioStop(function() {
      that.setData({
        isPlayingMusic: false
      });
      app.globalData.g_isPlayingMusic = false;
    });

    wx.onBackgroundAudioPlay(function(event) {
      //只处理当前页面的音乐播放；
      if (app.globalData.g_currentMusicPostId === that.postData.postId){
        that.setData({
          isPlayingMusic: true
        });
      }
      app.globalData.g_isPlayingMusic = true;
    });

    wx.onBackgroundAudioPause(function() {
      //只处理当前页面的音乐暂停；
      if (app.globalData.g_currentMusicPostId === that.postData.postId) {
        that.setData({
          isPlayingMusic: false
        });
      }
      app.globalData.g_isPlayingMusic = false;
    });
  },

  //初始化音乐图标状态;
  initMusicStatus() {
    var currentPostId = this.postData.postId;
    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusicPostId === currentPostId) {
      //如果全局播放的音乐是当前文章的音乐，就将图标状态设置为正在播放；
      this.setData({
        isPlayingMusic: true
      })
    }
    else{
      this.setData({
        isPlayingMusic: false
      })
    }
  },

  setAniation:function(){
    //定义动画
    var animationUp = wx.createAnimation({
      timingFunction:"ease-in-out"
    })

    this.animationUp = animationUp
  }

})